package com.lzxuni.controller;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lzxuni.po.QxData;
import com.lzxuni.po.QxDataCostom;
import com.lzxuni.po.YongHu;
import com.lzxuni.po.YongHuCustom;
import com.lzxuni.service.QxDataService;
import com.lzxuni.service.YongHuService;
import com.lzxuni.utils.JsonResultUtil;
import com.lzxuni.utils.MethodUtil;
import com.lzxuni.utils.UuidUtil;

@Controller
@RequestMapping("/ht")
public class YongHuController {
	@Autowired
	private YongHuService yongHuService;
	@Autowired
	private QxDataService qxDataService;

	// 部门列表
	@RequestMapping("/list.html")

	public ModelAndView list() throws Exception {
		ModelAndView mv = new ModelAndView("/yonghu/list");
		return mv;
	}
	// 部门列表
	@SuppressWarnings("unchecked")
	@RequestMapping("/listDo.html")
	public void listDo(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		int pageNumber = request.getParameter("pageNumber") == null ? 1 : Integer.parseInt(request.getParameter("pageNumber"));
		int pageSize = request.getParameter("pageSize") == null ? 10 : Integer.parseInt(request
				.getParameter("pageSize"));
		YongHuCustom yongHuCustom=new YongHuCustom();
		
		// 分页
		@SuppressWarnings("rawtypes")
		PageInfo pageInfo = yongHuService.findYongHuList(pageNumber, pageSize,yongHuCustom);
		List<YongHu> yongHulist = pageInfo.getList();
		// 返回json数组分页
		JSONObject jsonObject = new JSONObject();
		JSONArray array = new JSONArray();
		for (Iterator<YongHu> iterator = yongHulist.iterator(); iterator.hasNext();) {
			JSONObject jsonObject1 = new JSONObject();
			YongHu yongHu = iterator.next();
		    QxData qxData=qxDataService.findQxDataById(yongHu.getUserkey_id());
			String userkey ="";
			if(qxData!=null){
				userkey = qxData.getUserkey();
			}
			jsonObject1.put("id", yongHu.getId());
			jsonObject1.put("userkey_id", yongHu.getUserkey_id());
			jsonObject1.put("createTime", MethodUtil.formatDate(3,yongHu.getCreateTime()));
			jsonObject1.put("userName",yongHu.getUserName());
			jsonObject1.put("userkey",userkey);
			array.add(jsonObject1);
		}
		jsonObject.put("total", pageInfo.getTotal());
		jsonObject.put("rows", array);
		// 输出
		System.out.println(jsonObject.toString());
		MethodUtil.writer(response, jsonObject.toString());
	}


	

	// 部门添加展示页面
	@RequestMapping("/insert.html")
	public ModelAndView insert() throws Exception {

		QxDataCostom qxDataCostom=new QxDataCostom();
		List<QxDataCostom> qxDataList=qxDataService.findQxDataList(qxDataCostom);
		ModelAndView mv = new ModelAndView("/yonghu/insert");
		mv.addObject("qxDataList", qxDataList);
		return mv;
	}
		// 用户处理
	@ResponseBody
	@RequestMapping("/insertDo.html")
	public Object insertDo(YongHu yongHu,HttpServletRequest request) throws Exception {
	
		yongHu.setId(UuidUtil.get32UUID());
		yongHu.setCreateTime(MethodUtil.getDate(1, null));

		yongHuService.insertYongHu(yongHu);
		return JsonResultUtil.getSuccessJson("成功！");
	}

	// 部门
	@RequestMapping("/update.html")
	public ModelAndView update(String id) throws Exception {
		YongHu yongHu = yongHuService.findYongHuById(id);
		QxDataCostom qxDataCostom = new QxDataCostom();
		yongHu.setId(id);
		qxDataCostom.setId(yongHu.getId());
		
		List<QxDataCostom> qxDataList= qxDataService.findQxDataList(qxDataCostom);
		ModelAndView mv = new ModelAndView("/yonghu/update");
		mv.addObject("yongHu", yongHu);
		mv.addObject("qxDataList", qxDataList);
		return mv;
	}

	/*	//根据地市查询合作社
	@ResponseBody
	@RequestMapping("/deptld.html")
	public Object deptld(String areaId) throws Exception{
		YongHuCustom deptCustom=new YongHuCustom();
		Boolean flag=false;
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		for(int i=0;i<user.getRoleList().size();i++){
			//如果 role = 1  代表当前登录人有总管理员的权限
			if(user.getRoleList().get(i).getId().equals("1")){
				flag=true;
				break;
			}
		}
		if(!flag){
			deptCustom.setId(user.getDeptId());
		}
		deptCustom.setArea_id(areaId);
		List<YongHuCustom> deptList = deptService.findDeptList(deptCustom);
		return deptList;
	}*/

}
